<script type="text/javascript">
'use strict'
	// 对象绑定的函数, 称作对象的方法
	var xiaoming = {
		name: '小明',
		birth: 1990,
		age: function() {
			var y = new Date().getFullYear();
			return y - this.birth;
		}
	};
	console.log(xiaoming.age);
	console.log(xiaoming.age());

	// 一种错误, this 指向的是调用他的对象前提是这个对象绑定了这个方法, 如果没有指明对象, 则默认指向window这个对象, 在 strict 模式下指向了undefined

	function getAge() {
	    var y = new Date().getFullYear();
	    return y - this.birth;
	}

	var xiaoming = {
	    name: '小明',
	    birth: 1990,
	    age: getAge
	};


	// console.log(xiaoming.age()); // 错误
	// console.log(getAge()); // 错误

	// 第二种错误
	var xiaoming = {
		name: '小明',
		birth: 1990,
		age: function() {
			var y = new Date().getFullYear();
			return y - this.birth;
		}
	};
	var fn = xiaoming.age;
	// console.log(fn()) // 错误

	// that
	var xiaoming = {
		name: '小明',
		birth: 1990,
		age: function() {
			var that = this; // 用that捕获this, 因为在内部方法中this又指向了undefined
			function getAgeFromBirth() {
				var y = new Date().getFullYear();
				return y - that.birth;
			}
			return getAgeFromBirth();
		}
	}

	console.log(xiaoming.age());

	// apply 指定函数this的指向
	function getAge() {
		var y = new Date().getFullYear();
		return y - this.birth;
	}
	var xiaoming = {
		name: '小明',
		birth: 1990,
		age: getAge
	}
	console.log(getAge.apply(xiaoming, [])); // 指向对象小明, 参数为空
	console.log(getAge.call(xiaoming)); // call(object, arg, arg, arg,...), 调用形式不一样

	// 装饰器
	console.log(parseInt());
	var count = 0;
	var oldParseInt = parseInt; // 保存原函数

	window.parseInt = function() {
		count += 1;
		return oldParseInt.apply(null, arguments); // arguments 是待传的参数列表 ...rest居然不行
	}
	console.log(parseInt('10'));
	console.log(parseInt('20'));
	console.log(parseInt('30'));
	console.log(parseInt('40'));
	console.log('count=', count);


</script>